﻿using Dal;
using IBll;
using IDal;
using Model;
using System.Security.Cryptography.X509Certificates;

namespace Bll
{
    /// <summary>
    /// 房型继承接口
    /// </summary>
    public class StareBll : IStareBll
    {
        /// <summary>
        /// 实例化
        /// </summary>
        /// <param name="stare"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        /// 
        public StareBll(IStateDal<Stare> stateDa, IStateDal<StarePtea> state)
        {

            StateDa = stateDa;

            State = state;

        }

        public IStateDal<Stare> StateDa { get; }
        public IStateDal<StarePtea> State { get; }


        /// <summary>
        /// 查询分页
        /// </summary>
        /// <param name="name">房型名称</param>
        /// <param name="maxstareMax">房型大小</param>
        /// <param name="minstareMax">房型大小</param>
        /// <param name="index">页数</param>
        /// <param name="size">条数</param>
        /// <returns>查询数据</returns>
        public StareDto ShowStare(string? name, int? maxstareMax, int? minstareMax, int index, int size)
        {
            try//异常处理
            {
                var stare = StateDa.GetValues();
                var starePtea= State.GetValues();

                var linq = from a in stare
                           join b in starePtea
                           on a.StarePteaId equals b.StarePteaId
                           select new
                           {
                               a.StareId,
                               a.StareName,
                               a.StareDate,
                               a.StareType,
                               a.StareMax,
                               a.State,
                               a.Num,
                               a.StareImg,
                               b.StarePteaName,
                              a.StarePteaId

                           };



                if (!string.IsNullOrEmpty(name))
                {
                linq=  linq.Where(x => x.StareName.Contains(name));
                }

                if (maxstareMax>0)
                {
                    linq = linq.Where(x => x.StareMax>=maxstareMax);
                }

                if (minstareMax>0)
                {
                    linq = linq.Where(x => x.StareMax <= minstareMax);
                }

                int g = linq.Count();

                 var p=linq.OrderBy(x=>x.StareId).Skip((index-1)*size).Take(size).ToList();

                  return new StareDto {Count = g,Mumber=p};
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="stare">房型表</param>
        /// <returns>往房型表里面添加数据</returns>
        public  async  Task<int> AddAnyns(Stare stare)
        {
            try//异常处理
            {

                var lisya = StateDa.GetValues().FirstOrDefault(x => x.StareName == stare.StareName);
                 if(lisya==null)
                {
                    return -1;//房型名称已存在
         
                }
                return await StateDa.AddAnyns(stare);

            }
            catch (Exception)
            {

                throw;
            }
        }



        /// <summary>
        /// 返填
        /// </summary>
        /// <param name="id">主键id</param>
        /// <returns>通过id获取一条信息</returns>
        public Stare FindStare(int id)
        {
            try//异常处理
            {
                 return  StateDa.GetValues().FirstOrDefault(x=>x.StareId == id);
                  
            }
            catch (Exception)
            {

                throw;
            }
        }


 

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="stare">房型表</param>
        /// <returns>往房型表里面添加数据</returns>
        public async  Task<int> UpdateAnyns(Stare stare)
        {
            var lisya = StateDa.GetValues().FirstOrDefault(x => x.StareName == stare.StareName);
            if (lisya == null)
            {

                return await StateDa.UpdateAnyns(stare);

            }
            return -1;//房型名称已存在
        }
    }
}
